package info.javafans.servlet;

import info.javafans.dao.CategoryDao;
import info.javafans.dao.CommentDao;
import info.javafans.dao.PostDao;
import info.javafans.dao.UserDao;
import info.javafans.domain.Category;
import info.javafans.domain.Comment;
import info.javafans.domain.Post;
import info.javafans.domain.User;
import info.javafans.utils.DateTime;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class PostServlet
 */
public class PostServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PostServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");

		if (action == null) {

		} else if (action.equals("newPost")) {
			this.doNewPost(request, response);
		} else if (action.equals("addPost")) {
			this.doAddPost(request, response);
		} else if (action.equals("addCate")) {
			this.doAddCate(request, response);
		} else if (action.equals("editCate")) {
			this.doEditCate(request, response);
		} else if (action.equals("deleteCate")) {
			this.doDeleteCate(request, response);
		} else if (action.equals("addUser")) {
			this.doAddUser(request, response);
		} else if (action.equals("editUser")) {
			this.doEditUser(request, response);
		} else if (action.equals("deleteUser")) {
			this.doDeleteUser(request, response);
		}
	}

	//newPost
	private void doNewPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PostDao postDao = new PostDao();
		
		//获得文章分类列表--common
		CategoryDao categoryDao = new CategoryDao();
		List<Category> categoryList = categoryDao.selectAllCategory();
		request.setAttribute("categoryList", categoryList);
		
		//获得viewCount排名文章列表--common
		List<Post> viewRankList = postDao.selectMostViewTitle();
		request.setAttribute("viewRankList", viewRankList);
		
		//获得评论列表--common
		CommentDao commentDao = new CommentDao();
		List<Comment> newCommentList = commentDao.selectNewComments();
		request.setAttribute("newCommentList", newCommentList);
		
		//获得最新文章标题列表--common
		List<Post> newPostTitleList = postDao.selectNewPostTitle();
		request.setAttribute("newPostTitleList", newPostTitleList);
		
		request.getRequestDispatcher("newPost.jsp").forward(request, response);
	}
	//addPost
	private void doAddPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String title = request.getParameter("title");
		String categoryId = request.getParameter("category");
		String tags = request.getParameter("tags");
		String content = request.getParameter("content");
		
		Post post = new Post();
		Category category = new Category();
		category.setId(categoryId);
		post.setCategory(category);
		
		post.setTitle(title);
		post.setTags(tags);
		post.setContent(content);
		
		post.setDate(DateTime.getNowTime());
		
		User user = (User)request.getSession().getAttribute("user");
		post.setAuthor(user);
		
		new PostDao().addPost(post);
		response.sendRedirect("WelcomeServlet");
	}
	//addCate
	private void doAddCate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("category");
		
		Category c = new Category();
		c.setName(name);
		CategoryDao categoryDao = new CategoryDao();
		categoryDao.addCategory(c);
		
		response.sendRedirect("AdminServlet?action=manage");
	}
	//editCate
	private void doEditCate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		
		Category c = new Category();
		c.setId(id);
		c.setName(name);
		CategoryDao categoryDao = new CategoryDao();
		categoryDao.updateCategory(c);
		
		response.sendRedirect("AdminServlet?action=manage");
	}
	//deleteCate
	private void doDeleteCate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		CategoryDao categoryDao = new CategoryDao();
		categoryDao.deleteCategoryById(id);
		
		response.sendRedirect("AdminServlet?action=manage");
	}
	//addUser
	private void doAddUser(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String repassword = request.getParameter("repassword");
		if (!password.equals(repassword)) {
			request.setAttribute("error", "两次输入的密码不一致!");
			request.getRequestDispatcher("AdminServlet?action=manage").forward(request, response);
		}
		String nickname = request.getParameter("nickname");
		String email = request.getParameter("email");
		String status = request.getParameter("status");
		User user = new User();
		user.setUsername(username);
		user.setPassword(repassword);
		user.setNickname(nickname);
		user.setEmail(email);
		user.setStatus(new Integer(status));
		user.setRegistered(DateTime.getNowTimeStr());
		
		new UserDao().addUser(user);
		request.getRequestDispatcher("AdminServlet?action=manage").forward(request, response);
	}
	//editUser
	private void doEditUser(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String username = request.getParameter("username");
		String nickname = request.getParameter("nickname");
		String email = request.getParameter("email");
		String status = request.getParameter("status");
		
		User user = new User();
		user.setId(id);
		user.setUsername(username);
		user.setNickname(nickname);
		user.setEmail(email);
		user.setStatus(new Integer(status));
		
		new UserDao().editUser(user);
		response.sendRedirect("AdminServlet?action=manage");
	}
	//deleteUser
	private void doDeleteUser(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		new UserDao().deleteUser(id);
		response.sendRedirect("AdminServlet?action=manage");
	}
}
